﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Web.SessionState;


namespace Web.Admin.Info
{
    /// <summary>
    /// info 的摘要说明
    /// </summary>
    public class info : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            common.IsLogin();
            if (context.Request.QueryString["type"] != null)
            {
                string strType = context.Request.QueryString["type"].ToString();
                switch (strType)
                {
                  
                    case "HasReply": HasReply(context); break;
                    case "Get_InfoType": Get_InfoType(context); break;
                    case "Add_InfoType": Add_InfoType(context); break;
                    case "Update_InfoType": Update_InfoType(context); break;
                    case "DeleteByID_InfoType": DeleteByID_InfoType(context); break;

                    case "UpdateGetByID_Info": UpdateGetByID_Info(context); break;
                    case "GetByID_Info": GetByID_Info(context); break;
                    case "Add_Info": Add_Info(context); break;
                    case "Update_Info": Update_Info(context); break;
                    case "Update_InfoBroswerTime": Update_InfoBroswerTime(context); break;
                    case "DeleteByID_Info": DeleteByID_Info(context); break;

                    case "Add_Reply": Add_Reply(context); break;
                        
                        
                    case "Get_InfoNotice": Get_InfoNotice(context); break;
                    case "Update_InfoNotice": Update_InfoNotice(context); break;

                        
                }
            }

        }

      
        void HasReply(HttpContext context)
        {
            string ID = context.Request.QueryString["ID"].ToString();
            object obj = SQLHelper.ExecuteScalar("   SELECT COUNT(ID)  FROM dbo.InfoReply WHERE InfoID=" + ID);


            if (obj.ToString()=="0")
            {
                context.Response.Write("0");
            }
            else
            {
                context.Response.Write("1");
            }
        }



        void Get_InfoType(HttpContext context)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("   SELECT ID,strType as strName FROM dbo.InfoType order by ISDefault desc,numSort asc");
           
            DataTable dt = SQLHelper.ExecuteDataTable(strSql.ToString());

            if (dt != null && dt.Rows.Count != 0)
            {
                context.Response.Write(JsonHelper.ToJson(dt));
            }
            else
            {
                context.Response.Write("Error");
            }
        }

        void Add_InfoType(HttpContext context)
        {

            string strType = context.Request.Form["strType"].ToString();


            string strCount = "    SELECT COUNT(ID)  FROM dbo.InfoType WHERE strType='" + strType + "'";
            if (SQLHelper.ExecuteScalar(strCount).ToString() != "0")
            {
                context.Response.Write("1");
                return;
            }
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" INSERT INTO dbo.InfoType( strType, ISDefault, numSort )");
            strSql.Append(" VALUES  ( N'"+strType+"',  0,  1 )");

            if (SQLHelper.ExecuteNonQuery(strSql.ToString()) != 0)
            {
                context.Response.Write("OK");
            }
            else
            {
                context.Response.Write("Error");
            }
        }

        void Update_InfoType(HttpContext context)
        {
            string ID = context.Request.QueryString["ID"].ToString();
            string strType = context.Request.Form["strType"].ToString();

            StringBuilder strSql = new StringBuilder();
            strSql.Append(" UPDATE dbo.InfoType SET strType='"+strType+"' WHERE ID="+ID);
           

            if (SQLHelper.ExecuteNonQuery(strSql.ToString()) != 0)
            {
                context.Response.Write("OK");
            }
            else
            {
                context.Response.Write("Error");
            }
        }

        void DeleteByID_InfoType(HttpContext context)
        {
            string wbid = context.Request.QueryString["ID"].ToString();

            //查询该条目是否已经被使用
            if (SQLHelper.ExecuteScalar("SELECT COUNT(ID) FROM dbo.Info WHERE InfoTypeID=" + wbid).ToString() != "0")
            {
                context.Response.Write("Exit");
                return;
            }

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [InfoType] ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = wbid;


            if (SQLHelper.ExecuteNonQuery(strSql.ToString(), parameters) != 0)
            {
                context.Response.Write("OK");
            }
            else
            {
                context.Response.Write("Error");
            }
        }





        /// <summary>
        /// 更新浏览次数后在获取
        /// </summary>
        /// <param name="context"></param>
        void UpdateGetByID_Info(HttpContext context)
        {
            string ID = context.Request.QueryString["ID"].ToString();


            StringBuilder strSqlCount = new StringBuilder();
            strSqlCount.Append(" UPDATE dbo.Info  ");
            strSqlCount.Append(" SET BrowseTime=(SELECT BrowseTime+1  FROM dbo.Info WHERE ID=" + ID + ")");
            strSqlCount.Append(" WHERE dbo.Info.ID=" + ID);

            SQLHelper.ExecuteNonQuery(strSqlCount.ToString());

            StringBuilder strSql = new StringBuilder();
            strSql.Append("     select A.ID,InfoTypeID,B.strRealName AS  UserID,strTitle,strContent,ISStick,ISKeepSecret,BrowseTime,A.dt_Add,A.dt_Update ");
            strSql.Append("   FROM dbo.Info A INNER JOIN dbo.Users B ON A.UserID=B.ID ");
            strSql.Append("  WHERE A.ID=" + ID);
            DataTable dt = SQLHelper.ExecuteDataTable(strSql.ToString());

            if (dt != null && dt.Rows.Count != 0)
            {
                context.Response.Write(JsonHelper.ToJson(dt));
            }
            else
            {
                context.Response.Write("Error");
            }
        }

        void GetByID_Info(HttpContext context)
        {
            string ID = context.Request.QueryString["ID"].ToString();

            StringBuilder strSql = new StringBuilder();
            strSql.Append("     select A.ID,InfoTypeID,B.strRealName AS  UserID,strTitle,strContent,ISStick,ISKeepSecret,BrowseTime,A.dt_Add,A.dt_Update ");
            strSql.Append("   FROM dbo.Info A INNER JOIN dbo.Users B ON A.UserID=B.ID ");
            strSql.Append("  WHERE A.ID=" + ID);
            DataTable dt = SQLHelper.ExecuteDataTable(strSql.ToString());

            if (dt != null && dt.Rows.Count != 0)
            {
                context.Response.Write(JsonHelper.ToJson(dt));
            }
            else
            {
                context.Response.Write("Error");
            }
        }

        void Add_Info(HttpContext context)
        {

            string InfoTypeID = context.Request.Form["InfoTypeID"].ToString();
            string UserID = context.Session["ID"].ToString();
            string strTitle = context.Request.Form["strTitle"].ToString();
            string strContent = context.Request.Form["strContent"].ToString();
            bool ISStick = false;
            if (context.Request.Form["ISStick"] != null)
            {
                ISStick = true;
            }
            bool ISKeepSecret = false;
            if (context.Request.Form["ISKeepSecret"] != null)
            {
                ISKeepSecret = true;
            }

            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [Info] (");
            strSql.Append("InfoTypeID,UserID,strTitle,strContent,ISStick,ISKeepSecret,BrowseTime,dt_Add,dt_Update)");
            strSql.Append(" values (");
            strSql.Append("@InfoTypeID,@UserID,@strTitle,@strContent,@ISStick,@ISKeepSecret,@BrowseTime,@dt_Add,@dt_Update)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@InfoTypeID", SqlDbType.Int,4),
					new SqlParameter("@UserID", SqlDbType.Int,4),
					new SqlParameter("@strTitle", SqlDbType.NVarChar,100),
					new SqlParameter("@strContent", SqlDbType.NVarChar,4000),
					new SqlParameter("@ISStick", SqlDbType.Bit,1),
					new SqlParameter("@ISKeepSecret", SqlDbType.Bit,1),
					new SqlParameter("@BrowseTime", SqlDbType.Int,4),
					new SqlParameter("@dt_Add", SqlDbType.DateTime),
					new SqlParameter("@dt_Update", SqlDbType.DateTime)};
            parameters[0].Value = InfoTypeID;
            parameters[1].Value = UserID;
            parameters[2].Value = strTitle;
            parameters[3].Value = strContent;
            parameters[4].Value = ISStick;
            parameters[5].Value = ISKeepSecret;
            parameters[6].Value = 0;
            parameters[7].Value = DateTime.Now;
            parameters[8].Value = DateTime.Now;
            if (SQLHelper.ExecuteNonQuery(strSql.ToString(), parameters) != 0)
            {
                context.Response.Write("OK");
            }
            else
            {
                context.Response.Write("Error");
            }
        }

        void Update_Info(HttpContext context)
        {
            string ID = context.Request.QueryString["ID"].ToString();

            string InfoTypeID = context.Request.Form["InfoTypeID"].ToString();
            string strTitle = context.Request.Form["strTitle"].ToString();
            string strContent = context.Request.Form["strContent"].ToString();
            bool ISStick = false;
            if (context.Request.Form["strContent"] != null)
            {
                ISStick = true;
            }
            bool ISKeepSecret = false;
            if (context.Request.Form["ISKeepSecret"] != null)
            {
                ISKeepSecret = true;
            }


            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [Info] set ");
            strSql.Append("InfoTypeID=@InfoTypeID,");
            strSql.Append("strTitle=@strTitle,");
            strSql.Append("strContent=@strContent,");
            strSql.Append("ISStick=@ISStick,");
            strSql.Append("ISKeepSecret=@ISKeepSecret,");
            strSql.Append("dt_Update=@dt_Update");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@InfoTypeID", SqlDbType.Int,4),
					new SqlParameter("@strTitle", SqlDbType.NVarChar,100),
					new SqlParameter("@strContent", SqlDbType.NVarChar,4000),
					new SqlParameter("@ISStick", SqlDbType.Bit,1),
					new SqlParameter("@ISKeepSecret", SqlDbType.Bit,1),
					new SqlParameter("@dt_Update", SqlDbType.DateTime),
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = InfoTypeID;
            parameters[1].Value = strTitle;
            parameters[2].Value = strContent;
            parameters[3].Value = ISStick;
            parameters[4].Value = ISKeepSecret;
            parameters[5].Value = DateTime.Now;
            parameters[6].Value = ID;
            if (SQLHelper.ExecuteNonQuery(strSql.ToString(), parameters) != 0)
            {
                context.Response.Write("OK");
            }
            else
            {
                context.Response.Write("Error");
            }
        }
        void Update_InfoBroswerTime(HttpContext context)
        {
            string ID = context.Request.QueryString["ID"].ToString();

            

            StringBuilder strSql = new StringBuilder();
            strSql.Append(" UPDATE dbo.Info  ");
            strSql.Append(" SET BrowseTime=(SELECT BrowseTime+1  FROM dbo.Info WHERE ID="+ID+")");
            strSql.Append(" WHERE dbo.Info.ID="+ID);
           
            if (SQLHelper.ExecuteNonQuery(strSql.ToString()) != 0)
            {
                context.Response.Write("OK");
            }
            else
            {
                context.Response.Write("Error");
            }
        }


        void DeleteByID_Info(HttpContext context)
        {
            string wbid = context.Request.QueryString["ID"].ToString();

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [Info] ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = wbid;


            if (SQLHelper.ExecuteNonQuery(strSql.ToString(), parameters) != 0)
            {
                context.Response.Write("OK");
            }
            else
            {
                context.Response.Write("Error");
            }
        }

        /// <summary>
        /// 为一条信息添加评论
        /// </summary>
        /// <param name="context"></param>
        void Add_Reply(HttpContext context)
        {

            string InfoID = context.Request.QueryString["ID"].ToString();

            string UserID = context.Session["ID"].ToString();
            string strContent = context.Request.Form["strContent"].ToString();
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" INSERT  INTO dbo.InfoReply( InfoID ,UserID , strContent , dt_Reply )");
            strSql.Append(" VALUES  ( "+InfoID+" ,  "+UserID+" , N'"+strContent+"' ,  N'"+DateTime.Now+"'  )");
           
            if (SQLHelper.ExecuteNonQuery(strSql.ToString()) != 0)
            {
                context.Response.Write("OK");
            }
            else
            {
                context.Response.Write("Error");
            }
        }


        void Get_InfoNotice(HttpContext context)
        {
           
            StringBuilder strSql = new StringBuilder();
            strSql.Append("   SELECT top 1 strContent FROM dbo.InfoNotice");

            object obj = SQLHelper.ExecuteScalar(strSql.ToString());
            if (obj != null)
            {
                context.Response.Write(obj.ToString());
            }
            else
            {
                context.Response.Write("");
            }
        }
        void Update_InfoNotice(HttpContext context)
        {
            string strContent=context.Request.Form["strContent"].ToString();
            string strNoticeCount = SQLHelper.ExecuteScalar(" select count(ID) from InfoNotice").ToString();
            string strSql = "";
            if (strNoticeCount == "0")
            {
                strSql = " INSERT INTO dbo.InfoNotice ( strContent )VALUES  ( N'" + strContent + "' )";
            }
            else {
                strSql = " UPDATE dbo.InfoNotice SET strContent='"+strContent+"'";
            }
            
            if (SQLHelper.ExecuteNonQuery(strSql) != 0)
            {
                context.Response.Write("OK");
            }
            else
            {
                context.Response.Write("Error");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}